package team.nmsg.ge.system.init;
import java.util.Map.Entry;
import java.util.Set;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import team.nmsg.ge.system.util.ServerParams;
import team.nmsg.photo.PhotoUtil;

@Configuration
public class NMSWebAppConfigurer  extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    	
    	String userDir = System.getProperty("user.dir");
    	String replace = userDir.replace("\\", "/");
    	
    	
        ResourceHandlerRegistration resourceHandler = registry.addResourceHandler("/resources/**");
        resourceHandler.addResourceLocations("file:"+replace+"/resources/");
        
        Set<Entry<String,String>> entrySet = PhotoUtil.resToPath.entrySet();
        for(  Entry<String,String> en : entrySet  ) {
        	String res = en.getKey();
        	String dir = en.getValue();
        	ResourceHandlerRegistration resTmp = registry.addResourceHandler( res + "**");
        	resTmp.addResourceLocations("file:"+ dir );
        }
        
        super.addResourceHandlers(registry);
        
    }
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOrigins("*")
                .allowedMethods("*").allowedHeaders("*")
                .allowCredentials(true)
                .exposedHeaders(HttpHeaders.SET_COOKIE).maxAge(3600L);
    }

    
    public static void main(String[] args) {
		
    	String a = "E:\\D\\OrderWorkSpace\\nmsweb_root\\nmsweb_RestBootstrap";
    	
    	System.out.println(  a.replace("\\\\", "/") );
    	
	}
    
    
}